{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pandas:数据的分组和聚合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "内容介绍:\n",
    "\n",
    "数据分组汇总演示图\n",
    "\n",
    " ![avatar](combine.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         orgId category    code pinyin  zwjc\n",
      "0  gssz0000001       A股  000001   payh  平安银行\n",
      "1  gssz0000002       A股  000002    wka   万科A\n",
      "2  gssz0000004       A股  000004   ghwa  国华网安\n",
      "3  gssz0000005       A股  000005   stxy  ST星源\n",
      "4  gssz0000006       A股  000006   szya  深振业A\n"
     ]
    }
   ],
   "source": [
    "# 示例数据\n",
    "# 使用 Python JSON 模块载入数据\n",
    "with open('szse_stock.json','rb') as f:\n",
    "    data = json.loads(f.read())\n",
    "\n",
    "# 展平数据\n",
    "df = pd.json_normalize(data, record_path =['stockList'])\n",
    "print(df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>Unnamed: 0.1</th>\n",
       "      <th>Brand</th>\n",
       "      <th>Store Number</th>\n",
       "      <th>Store Name</th>\n",
       "      <th>Ownership Type</th>\n",
       "      <th>Street Address</th>\n",
       "      <th>City</th>\n",
       "      <th>State/Province</th>\n",
       "      <th>Country</th>\n",
       "      <th>Postcode</th>\n",
       "      <th>Phone Number</th>\n",
       "      <th>Timezone</th>\n",
       "      <th>Longitude</th>\n",
       "      <th>Latitude</th>\n",
       "      <th>AvgScore</th>\n",
       "      <th>Score</th>\n",
       "      <th>Id</th>\n",
       "      <th>Relativity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>Starbucks</td>\n",
       "      <td>47370-257954</td>\n",
       "      <td>Meritxell, 96</td>\n",
       "      <td>Licensed</td>\n",
       "      <td>Av. Meritxell, 96</td>\n",
       "      <td>Andorra la Vella</td>\n",
       "      <td>7</td>\n",
       "      <td>AD</td>\n",
       "      <td>AD500</td>\n",
       "      <td>376818720</td>\n",
       "      <td>GMT+1:00 Europe/Andorra</td>\n",
       "      <td>1.53</td>\n",
       "      <td>42.51</td>\n",
       "      <td>6.5</td>\n",
       "      <td>8 10 2 6</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Starbucks</td>\n",
       "      <td>22331-212325</td>\n",
       "      <td>Ajman Drive Thru</td>\n",
       "      <td>Licensed</td>\n",
       "      <td>1 Street 69, Al Jarf</td>\n",
       "      <td>Ajman</td>\n",
       "      <td>AJ</td>\n",
       "      <td>AE</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>GMT+04:00 Asia/Dubai</td>\n",
       "      <td>55.47</td>\n",
       "      <td>25.42</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>Starbucks</td>\n",
       "      <td>47089-256771</td>\n",
       "      <td>Dana Mall</td>\n",
       "      <td>Licensed</td>\n",
       "      <td>Sheikh Khalifa Bin Zayed St.</td>\n",
       "      <td>Ajman</td>\n",
       "      <td>AJ</td>\n",
       "      <td>AE</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>GMT+04:00 Asia/Dubai</td>\n",
       "      <td>55.47</td>\n",
       "      <td>25.39</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>Starbucks</td>\n",
       "      <td>22126-218024</td>\n",
       "      <td>Twofour 54</td>\n",
       "      <td>Licensed</td>\n",
       "      <td>Al Salam Street</td>\n",
       "      <td>Abu Dhabi</td>\n",
       "      <td>AZ</td>\n",
       "      <td>AE</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>GMT+04:00 Asia/Dubai</td>\n",
       "      <td>54.38</td>\n",
       "      <td>24.48</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>Starbucks</td>\n",
       "      <td>17127-178586</td>\n",
       "      <td>Al Ain Tower</td>\n",
       "      <td>Licensed</td>\n",
       "      <td>Khaldiya Area, Abu Dhabi Island</td>\n",
       "      <td>Abu Dhabi</td>\n",
       "      <td>AZ</td>\n",
       "      <td>AE</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>GMT+04:00 Asia/Dubai</td>\n",
       "      <td>54.54</td>\n",
       "      <td>24.51</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0  Unnamed: 0.1      Brand  Store Number        Store Name  \\\n",
       "0           0             0  Starbucks  47370-257954     Meritxell, 96   \n",
       "1           1             1  Starbucks  22331-212325  Ajman Drive Thru   \n",
       "2           2             2  Starbucks  47089-256771         Dana Mall   \n",
       "3           3             3  Starbucks  22126-218024        Twofour 54   \n",
       "4           4             4  Starbucks  17127-178586      Al Ain Tower   \n",
       "\n",
       "  Ownership Type                   Street Address              City  \\\n",
       "0       Licensed                Av. Meritxell, 96  Andorra la Vella   \n",
       "1       Licensed             1 Street 69, Al Jarf             Ajman   \n",
       "2       Licensed     Sheikh Khalifa Bin Zayed St.             Ajman   \n",
       "3       Licensed                  Al Salam Street         Abu Dhabi   \n",
       "4       Licensed  Khaldiya Area, Abu Dhabi Island         Abu Dhabi   \n",
       "\n",
       "  State/Province Country Postcode Phone Number                 Timezone  \\\n",
       "0              7      AD    AD500    376818720  GMT+1:00 Europe/Andorra   \n",
       "1             AJ      AE      NaN          NaN     GMT+04:00 Asia/Dubai   \n",
       "2             AJ      AE      NaN          NaN     GMT+04:00 Asia/Dubai   \n",
       "3             AZ      AE      NaN          NaN     GMT+04:00 Asia/Dubai   \n",
       "4             AZ      AE      NaN          NaN     GMT+04:00 Asia/Dubai   \n",
       "\n",
       "   Longitude  Latitude  AvgScore      Score  Id  Relativity  \n",
       "0       1.53     42.51       6.5  8 10 2 6    0           0  \n",
       "1      55.47     25.42       0.0        NaN   1           0  \n",
       "2      55.47     25.39       0.0        NaN   2           0  \n",
       "3      54.38     24.48       0.0        NaN   3           0  \n",
       "4      54.54     24.51       0.0        NaN   4           0  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df0 = pd.read_csv('directory.csv')\n",
    "df0.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>科目</th>\n",
       "      <th>语文</th>\n",
       "      <th>英语</th>\n",
       "      <th>数学</th>\n",
       "      <th>班级</th>\n",
       "      <th>性别</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>姓名</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>小刚</th>\n",
       "      <td>86</td>\n",
       "      <td>51</td>\n",
       "      <td>62</td>\n",
       "      <td>二班</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小花</th>\n",
       "      <td>58</td>\n",
       "      <td>95</td>\n",
       "      <td>60</td>\n",
       "      <td>一班</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小敏</th>\n",
       "      <td>63</td>\n",
       "      <td>67</td>\n",
       "      <td>51</td>\n",
       "      <td>二班</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小强</th>\n",
       "      <td>89</td>\n",
       "      <td>68</td>\n",
       "      <td>66</td>\n",
       "      <td>一班</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小明</th>\n",
       "      <td>69</td>\n",
       "      <td>93</td>\n",
       "      <td>50</td>\n",
       "      <td>一班</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "科目  语文  英语  数学  班级 性别\n",
       "姓名                   \n",
       "小刚  86  51  62  二班  男\n",
       "小花  58  95  60  一班  女\n",
       "小敏  63  67  51  二班  男\n",
       "小强  89  68  66  一班  女\n",
       "小明  69  93  50  一班  男"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx = pd.Index(['小刚','小花','小敏','小强','小明'],name='姓名')\n",
    "cls = pd.Index(['语文','英语','数学'],name='科目')\n",
    "df00 = pd.DataFrame(np.random.randint(50,98,size=(5,3)),index=idx,columns=cls)\n",
    "df00['班级']=['二班','一班','二班','一班','一班']\n",
    "df00['性别']=['男','女','男','女','男']\n",
    "df00"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.groupby()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "#groupby()函数返回迭代对象。包含组名的元组序列\n",
    "g = df00.groupby(by='班级')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('一班', 科目  语文  英语  数学  班级 性别\n",
      "姓名                   \n",
      "小花  58  95  60  一班  女\n",
      "小强  89  68  66  一班  女\n",
      "小明  69  93  50  一班  男)\n",
      "('二班', 科目  语文  英语  数学  班级 性别\n",
      "姓名                   \n",
      "小刚  86  51  62  二班  男\n",
      "小敏  63  67  51  二班  男)\n"
     ]
    }
   ],
   "source": [
    "for i in g:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "组名:一班\n",
      "********************\n",
      "科目  语文  英语  数学  班级 性别\n",
      "姓名                   \n",
      "小花  58  95  60  一班  女\n",
      "小强  89  68  66  一班  女\n",
      "小明  69  93  50  一班  男\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "组名:二班\n",
      "********************\n",
      "科目  语文  英语  数学  班级 性别\n",
      "姓名                   \n",
      "小刚  86  51  62  二班  男\n",
      "小敏  63  67  51  二班  男\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "source": [
    "for bj,da in g:\n",
    "    print('组名:{}'.format(bj))\n",
    "    print('*'*20)\n",
    "    print(da)\n",
    "    print(type(da))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>科目</th>\n",
       "      <th>语文</th>\n",
       "      <th>英语</th>\n",
       "      <th>数学</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>班级</th>\n",
       "      <th>性别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">一班</th>\n",
       "      <th>女</th>\n",
       "      <td>73.5</td>\n",
       "      <td>81.5</td>\n",
       "      <td>63.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>男</th>\n",
       "      <td>69.0</td>\n",
       "      <td>93.0</td>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>二班</th>\n",
       "      <th>男</th>\n",
       "      <td>74.5</td>\n",
       "      <td>59.0</td>\n",
       "      <td>56.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "科目       语文    英语    数学\n",
       "班级 性别                  \n",
       "一班 女   73.5  81.5  63.0\n",
       "   男   69.0  93.0  50.0\n",
       "二班 男   74.5  59.0  56.5"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用两个条件分组\n",
    "g2 = df00.groupby(by=['班级','性别']).mean()\n",
    "g2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.frame.DataFrame"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(g2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.聚合方法\n",
    "\n",
    "groupby()形成的对象，本身具有计算聚合值的方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>科目</th>\n",
       "      <th>语文</th>\n",
       "      <th>英语</th>\n",
       "      <th>数学</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>班级</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>一班</th>\n",
       "      <td>72.0</td>\n",
       "      <td>85.333333</td>\n",
       "      <td>58.666667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>二班</th>\n",
       "      <td>74.5</td>\n",
       "      <td>59.000000</td>\n",
       "      <td>56.500000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "科目    语文         英语         数学\n",
       "班级                            \n",
       "一班  72.0  85.333333  58.666667\n",
       "二班  74.5  59.000000  56.500000"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取班级的平均分。\n",
    "g = df00.groupby(by=['班级'])\n",
    "g.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "班级\n",
       "一班    72.0\n",
       "二班    74.5\n",
       "Name: 语文, dtype: float64"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#也可以在group对象后面获取单独的列，进行计算\n",
    "g['语文'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "班级\n",
       "一班    72.0\n",
       "二班    74.5\n",
       "Name: 语文, dtype: float64"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#在分组返回值的基础上语法糖的简化写法\n",
    "df00['语文'].groupby(df00['班级']).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>科目</th>\n",
       "      <th>班级</th>\n",
       "      <th>语文</th>\n",
       "      <th>英语</th>\n",
       "      <th>数学</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>一班</td>\n",
       "      <td>72.0</td>\n",
       "      <td>85.333333</td>\n",
       "      <td>58.666667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>二班</td>\n",
       "      <td>74.5</td>\n",
       "      <td>59.000000</td>\n",
       "      <td>56.500000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "科目  班级    语文         英语         数学\n",
       "0   一班  72.0  85.333333  58.666667\n",
       "1   二班  74.5  59.000000  56.500000"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#as_index参数的使用方法，优化显示结果的索引\n",
    "df00.groupby(by=['班级'],as_index=False).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.自定义聚合函数的方法???"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>科目</th>\n",
       "      <th>语文</th>\n",
       "      <th>英语</th>\n",
       "      <th>数学</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>班级</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>一班</th>\n",
       "      <td>31</td>\n",
       "      <td>27</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>二班</th>\n",
       "      <td>23</td>\n",
       "      <td>16</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "科目  语文  英语  数学\n",
       "班级            \n",
       "一班  31  27  16\n",
       "二班  23  16  11"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算每个人的分数同班级平均分的差\n",
    "def my_diff(arr):\n",
    "    return arr.max()-arr.min()\n",
    "\n",
    "#agg/aggregate\n",
    "df00.groupby(by=['班级']).agg(my_diff)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.数据处理拓展\n",
    "\n",
    "针对星巴克门店数据的初步分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>Unnamed: 0.1</th>\n",
       "      <th>Brand</th>\n",
       "      <th>Store Number</th>\n",
       "      <th>Store Name</th>\n",
       "      <th>Ownership Type</th>\n",
       "      <th>Street Address</th>\n",
       "      <th>City</th>\n",
       "      <th>State/Province</th>\n",
       "      <th>Postcode</th>\n",
       "      <th>Phone Number</th>\n",
       "      <th>Timezone</th>\n",
       "      <th>Longitude</th>\n",
       "      <th>Latitude</th>\n",
       "      <th>AvgScore</th>\n",
       "      <th>Score</th>\n",
       "      <th>Id</th>\n",
       "      <th>Relativity</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Country</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>AD</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AE</th>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>24</td>\n",
       "      <td>78</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>0</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AR</th>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>100</td>\n",
       "      <td>29</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>0</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AT</th>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>17</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>0</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AU</th>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TT</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TW</th>\n",
       "      <td>394</td>\n",
       "      <td>394</td>\n",
       "      <td>394</td>\n",
       "      <td>394</td>\n",
       "      <td>394</td>\n",
       "      <td>394</td>\n",
       "      <td>394</td>\n",
       "      <td>394</td>\n",
       "      <td>394</td>\n",
       "      <td>365</td>\n",
       "      <td>39</td>\n",
       "      <td>394</td>\n",
       "      <td>394</td>\n",
       "      <td>394</td>\n",
       "      <td>394</td>\n",
       "      <td>0</td>\n",
       "      <td>394</td>\n",
       "      <td>394</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>US</th>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13607</td>\n",
       "      <td>13122</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>1</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>VN</th>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>23</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>0</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ZA</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>73 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         Unnamed: 0  Unnamed: 0.1  Brand  Store Number  Store Name  \\\n",
       "Country                                                              \n",
       "AD                1             1      1             1           1   \n",
       "AE              144           144    144           144         144   \n",
       "AR              108           108    108           108         108   \n",
       "AT               18            18     18            18          18   \n",
       "AU               22            22     22            22          22   \n",
       "...             ...           ...    ...           ...         ...   \n",
       "TT                3             3      3             3           3   \n",
       "TW              394           394    394           394         394   \n",
       "US            13608         13608  13608         13608       13608   \n",
       "VN               25            25     25            25          25   \n",
       "ZA                3             3      3             3           3   \n",
       "\n",
       "         Ownership Type  Street Address   City  State/Province  Postcode  \\\n",
       "Country                                                                    \n",
       "AD                    1               1      1               1         1   \n",
       "AE                  144             144    144             144        24   \n",
       "AR                  108             108    108             108       100   \n",
       "AT                   18              18     18              18        18   \n",
       "AU                   22              22     22              22        22   \n",
       "...                 ...             ...    ...             ...       ...   \n",
       "TT                    3               3      3               3         3   \n",
       "TW                  394             394    394             394       365   \n",
       "US                13608           13608  13608           13608     13607   \n",
       "VN                   25              25     25              25        25   \n",
       "ZA                    3               3      3               3         3   \n",
       "\n",
       "         Phone Number  Timezone  Longitude  Latitude  AvgScore  Score     Id  \\\n",
       "Country                                                                        \n",
       "AD                  1         1          1         1         1      1      1   \n",
       "AE                 78       144        144       144       144      0    144   \n",
       "AR                 29       108        108       108       108      0    108   \n",
       "AT                 17        18         18        18        18      0     18   \n",
       "AU                  0        22         22        22        22      0     22   \n",
       "...               ...       ...        ...       ...       ...    ...    ...   \n",
       "TT                  0         3          3         3         3      0      3   \n",
       "TW                 39       394        394       394       394      0    394   \n",
       "US              13122     13608      13608     13608     13608      1  13608   \n",
       "VN                 23        25         25        25        25      0     25   \n",
       "ZA                  2         3          3         3         3      0      3   \n",
       "\n",
       "         Relativity  \n",
       "Country              \n",
       "AD                1  \n",
       "AE              144  \n",
       "AR              108  \n",
       "AT               18  \n",
       "AU               22  \n",
       "...             ...  \n",
       "TT                3  \n",
       "TW              394  \n",
       "US            13608  \n",
       "VN               25  \n",
       "ZA                3  \n",
       "\n",
       "[73 rows x 18 columns]"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g4 = df0.groupby('Country').count()\n",
    "g4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>Unnamed: 0.1</th>\n",
       "      <th>Brand</th>\n",
       "      <th>Store Number</th>\n",
       "      <th>Store Name</th>\n",
       "      <th>Ownership Type</th>\n",
       "      <th>Street Address</th>\n",
       "      <th>City</th>\n",
       "      <th>State/Province</th>\n",
       "      <th>Postcode</th>\n",
       "      <th>Phone Number</th>\n",
       "      <th>Timezone</th>\n",
       "      <th>Longitude</th>\n",
       "      <th>Latitude</th>\n",
       "      <th>AvgScore</th>\n",
       "      <th>Score</th>\n",
       "      <th>Id</th>\n",
       "      <th>Relativity</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Country</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>AD</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AE</th>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>24</td>\n",
       "      <td>78</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "      <td>0</td>\n",
       "      <td>144</td>\n",
       "      <td>144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AR</th>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>100</td>\n",
       "      <td>29</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "      <td>0</td>\n",
       "      <td>108</td>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AT</th>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>17</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>0</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AU</th>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AW</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AZ</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BE</th>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BG</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BH</th>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>2</td>\n",
       "      <td>13</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BN</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BO</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BR</th>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "      <td>73</td>\n",
       "      <td>43</td>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "      <td>0</td>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BS</th>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CA</th>\n",
       "      <td>1468</td>\n",
       "      <td>1468</td>\n",
       "      <td>1468</td>\n",
       "      <td>1468</td>\n",
       "      <td>1468</td>\n",
       "      <td>1468</td>\n",
       "      <td>1468</td>\n",
       "      <td>1468</td>\n",
       "      <td>1468</td>\n",
       "      <td>1464</td>\n",
       "      <td>1314</td>\n",
       "      <td>1468</td>\n",
       "      <td>1468</td>\n",
       "      <td>1468</td>\n",
       "      <td>1468</td>\n",
       "      <td>0</td>\n",
       "      <td>1468</td>\n",
       "      <td>1468</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CH</th>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>55</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>0</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CL</th>\n",
       "      <td>96</td>\n",
       "      <td>96</td>\n",
       "      <td>96</td>\n",
       "      <td>96</td>\n",
       "      <td>96</td>\n",
       "      <td>96</td>\n",
       "      <td>96</td>\n",
       "      <td>96</td>\n",
       "      <td>96</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>96</td>\n",
       "      <td>96</td>\n",
       "      <td>96</td>\n",
       "      <td>96</td>\n",
       "      <td>0</td>\n",
       "      <td>96</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CN</th>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2192</td>\n",
       "      <td>1337</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>1</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CO</th>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CR</th>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Unnamed: 0  Unnamed: 0.1  Brand  Store Number  Store Name  \\\n",
       "Country                                                              \n",
       "AD                1             1      1             1           1   \n",
       "AE              144           144    144           144         144   \n",
       "AR              108           108    108           108         108   \n",
       "AT               18            18     18            18          18   \n",
       "AU               22            22     22            22          22   \n",
       "AW                3             3      3             3           3   \n",
       "AZ                4             4      4             4           4   \n",
       "BE               19            19     19            19          19   \n",
       "BG                5             5      5             5           5   \n",
       "BH               21            21     21            21          21   \n",
       "BN                5             5      5             5           5   \n",
       "BO                4             4      4             4           4   \n",
       "BR              102           102    102           102         102   \n",
       "BS               10            10     10            10          10   \n",
       "CA             1468          1468   1468          1468        1468   \n",
       "CH               61            61     61            61          61   \n",
       "CL               96            96     96            96          96   \n",
       "CN             2734          2734   2734          2734        2734   \n",
       "CO               11            11     11            11          11   \n",
       "CR               11            11     11            11          11   \n",
       "\n",
       "         Ownership Type  Street Address  City  State/Province  Postcode  \\\n",
       "Country                                                                   \n",
       "AD                    1               1     1               1         1   \n",
       "AE                  144             144   144             144        24   \n",
       "AR                  108             108   108             108       100   \n",
       "AT                   18              18    18              18        18   \n",
       "AU                   22              22    22              22        22   \n",
       "AW                    3               3     3               3         0   \n",
       "AZ                    4               4     4               4         3   \n",
       "BE                   19              19    19              19        19   \n",
       "BG                    5               5     5               5         1   \n",
       "BH                   21              21    21              21         2   \n",
       "BN                    5               5     5               5         4   \n",
       "BO                    4               4     4               4         1   \n",
       "BR                  102             102   102             102        73   \n",
       "BS                   10              10    10              10         2   \n",
       "CA                 1468            1468  1468            1468      1464   \n",
       "CH                   61              61    61              61        61   \n",
       "CL                   96              96    96              96        45   \n",
       "CN                 2734            2734  2734            2734      2192   \n",
       "CO                   11              11    11              11         9   \n",
       "CR                   11              11    11              11         0   \n",
       "\n",
       "         Phone Number  Timezone  Longitude  Latitude  AvgScore  Score    Id  \\\n",
       "Country                                                                       \n",
       "AD                  1         1          1         1         1      1     1   \n",
       "AE                 78       144        144       144       144      0   144   \n",
       "AR                 29       108        108       108       108      0   108   \n",
       "AT                 17        18         18        18        18      0    18   \n",
       "AU                  0        22         22        22        22      0    22   \n",
       "AW                  3         3          3         3         3      0     3   \n",
       "AZ                  4         4          4         4         4      0     4   \n",
       "BE                  1        19         19        19        19      0    19   \n",
       "BG                  0         5          5         5         5      0     5   \n",
       "BH                 13        21         21        21        21      0    21   \n",
       "BN                  0         5          5         5         5      0     5   \n",
       "BO                  0         4          4         4         4      0     4   \n",
       "BR                 43       102        102       102       102      0   102   \n",
       "BS                  0        10         10        10        10      0    10   \n",
       "CA               1314      1468       1468      1468      1468      0  1468   \n",
       "CH                 55        61         61        61        61      0    61   \n",
       "CL                  1        96         96        96        96      0    96   \n",
       "CN               1337      2734       2734      2734      2734      1  2734   \n",
       "CO                  1        11         11        11        11      0    11   \n",
       "CR                  0        11         11        11        11      0    11   \n",
       "\n",
       "         Relativity  \n",
       "Country              \n",
       "AD                1  \n",
       "AE              144  \n",
       "AR              108  \n",
       "AT               18  \n",
       "AU               22  \n",
       "AW                3  \n",
       "AZ                4  \n",
       "BE               19  \n",
       "BG                5  \n",
       "BH               21  \n",
       "BN                5  \n",
       "BO                4  \n",
       "BR              102  \n",
       "BS               10  \n",
       "CA             1468  \n",
       "CH               61  \n",
       "CL               96  \n",
       "CN             2734  \n",
       "CO               11  \n",
       "CR               11  "
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g4.iloc[:20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.frame.DataFrame"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(g4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>Unnamed: 0.1</th>\n",
       "      <th>Brand</th>\n",
       "      <th>Store Number</th>\n",
       "      <th>Store Name</th>\n",
       "      <th>Ownership Type</th>\n",
       "      <th>Street Address</th>\n",
       "      <th>City</th>\n",
       "      <th>State/Province</th>\n",
       "      <th>Postcode</th>\n",
       "      <th>Phone Number</th>\n",
       "      <th>Timezone</th>\n",
       "      <th>Longitude</th>\n",
       "      <th>Latitude</th>\n",
       "      <th>AvgScore</th>\n",
       "      <th>Score</th>\n",
       "      <th>Id</th>\n",
       "      <th>Relativity</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Country</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>US</th>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13607</td>\n",
       "      <td>13122</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "      <td>1</td>\n",
       "      <td>13608</td>\n",
       "      <td>13608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CN</th>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2192</td>\n",
       "      <td>1337</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "      <td>1</td>\n",
       "      <td>2734</td>\n",
       "      <td>2734</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Unnamed: 0  Unnamed: 0.1  Brand  Store Number  Store Name  \\\n",
       "Country                                                              \n",
       "US            13608         13608  13608         13608       13608   \n",
       "CN             2734          2734   2734          2734        2734   \n",
       "\n",
       "         Ownership Type  Street Address   City  State/Province  Postcode  \\\n",
       "Country                                                                    \n",
       "US                13608           13608  13608           13608     13607   \n",
       "CN                 2734            2734   2734            2734      2192   \n",
       "\n",
       "         Phone Number  Timezone  Longitude  Latitude  AvgScore  Score     Id  \\\n",
       "Country                                                                        \n",
       "US              13122     13608      13608     13608     13608      1  13608   \n",
       "CN               1337      2734       2734      2734      2734      1   2734   \n",
       "\n",
       "         Relativity  \n",
       "Country              \n",
       "US            13608  \n",
       "CN             2734  "
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g4.loc[['US','CN']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.frame.DataFrame"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g4.size"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
